package com.ztc.ZtcSpecialRecommendation.web;

import com.alibaba.fastjson.JSONObject;
import com.aqgj.Attachment.bean.Attachment;
import com.aqgj.Attachment.service.AttachmentManager;
import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.aqgj.util.StringUtils;
import com.ztc.ZtcSpecialRecommendation.bean.ZtcSpecialRecommendation;
import com.ztc.ZtcSpecialRecommendation.service.ZtcSpecialRecommendationManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("ztc/special")
public class ZtcSpecialRecommendationAction {

    @Autowired
    private ZtcSpecialRecommendationManager ztcSpecialRecommendationManager;
    @Autowired
    private AttachmentManager attachmentManager;


    /**
     * 搜索线路站点信息（新增特价推荐）
     * @param startSite  起始站点
     * @param endSite    到达站点
     * @return
     */
    @ResponseBody
    @RequestMapping("searchClassForSale")
    public HashMap<String,Object> searchClassForSale(String startSite, String endSite){
        if (StringUtils.isBlank(startSite)||StringUtils.isBlank(endSite)) {
            return JsonWrapper.failureWrapperMsg("请输入起始站点或到达站点！");
        }
        return ztcSpecialRecommendationManager.searchClassForSale(startSite,endSite);
    }


    /**
     * 特价推荐线路save.
     * @param json       json信息（包含起点ID，终点ID，线路ID）
     * @param imgRemark  特价推荐img说明
     * @param type       类型   1 特价推荐，2热门推荐（无图片说明）
     * @return
     */
    @RequestMapping("saveSpecialRecommendation")
    @ResponseBody
    public HashMap<String,Object> saveSpecialRecommendation(String json, String imgRemark, @RequestParam(defaultValue = "1") Integer type, HttpSession session){
        if(StringUtils.isBlank(json)){
            return JsonWrapper.failureWrapperMsg("对象参数不完整！");
        }
        User user=(User) session.getAttribute("userInfo");
        if(user==null){
            return JsonWrapper.failureWrapperMsg("网络错误，请稍后重试！");
        }
        JSONObject jsonObject= JSONObject.parseObject(json);
        if(jsonObject.size()<3){
            return JsonWrapper.failureWrapperMsg("对象参数不完整！");
        }
        if (!jsonObject.containsKey("startId")||!jsonObject.containsKey("endId")||!jsonObject.containsKey("routeId")) {
          return JsonWrapper.failureWrapperMsg("对象参数不完整！");
        }
        if(type!=1&&type!=2){
            return JsonWrapper.failureWrapperMsg("类型参数错误！");
        }
        List<ZtcSpecialRecommendation> list= ztcSpecialRecommendationManager.getZtcSpecialListByRoutId(jsonObject.get("routeId").toString(),jsonObject.get("startId").toString(),jsonObject.get("endId").toString(),type);
        if(list.size()>0){
            return JsonWrapper.failureWrapperMsg("添加失败，此线路搜索站点已添加推荐！");
        }
        ZtcSpecialRecommendation ztcSpecialRecommendation=new ZtcSpecialRecommendation();
        ztcSpecialRecommendation.setType(type);
        ztcSpecialRecommendation.setStatus(1);//正常
        ztcSpecialRecommendation.setFkZtcRouteId(jsonObject.get("routeId").toString());
        ztcSpecialRecommendation.setFkRouteEndSiteId(jsonObject.get("endId").toString());
        ztcSpecialRecommendation.setFkRouteStartSiteId(jsonObject.get("startId").toString());
        ztcSpecialRecommendation.setCreateTime(new Date());
        String zid=  ztcSpecialRecommendationManager.save(ztcSpecialRecommendation);
        if(zid==null){
            return JsonWrapper.failureWrapperMsg("保存失败，请稍后重试！");
        }
        if(type==1){
            if(imgRemark==null||"".equals(imgRemark.trim())){
                return JsonWrapper.failureWrapperMsg("请上传特价推荐图片信息！");
            }
            Attachment attachment=new Attachment();
            attachment.setWorkItem("specialImg");
            attachment.setUploadTime(new Date());
            attachment.setUploaderName("线路特价推荐");
            attachment.setUploader(user.getId());
            attachment.setBusinessId(zid);
            String[] arrays = imgRemark.split("\\,");
            if (arrays.length < 3) {
                ztcSpecialRecommendationManager.delete(zid);
                return JsonWrapper.failureWrapperMsg("图片参数错误，添加失败!");
            }
            attachment.setFileType(arrays[0].split(":")[1].split(";")[0]);
            attachment.setFileName(arrays[2]);
            attachment.setFileBody(attachmentManager.decoderBase64(arrays[1]));
            attachmentManager.save(attachment);
        }
        return JsonWrapper.successWrapper("操作成功！");
    }

    /**
     * 获取推荐线路列表.
     * @return
     */
    @RequestMapping("getSpecialList")
    @ResponseBody
    public HashMap<String,Object> getSpecialList(@RequestParam Integer type){
        return ztcSpecialRecommendationManager.getSpecialALLList(type);
    }

    /**
     * 删除推荐.
     * @return
     */
    @RequestMapping("removeSpecial")
    @ResponseBody
    public HashMap<String,Object> removeSpecial(String id){
        if(StringUtils.isBlank(id)){
            return JsonWrapper.failureWrapperMsg("参数错误，ID为空！");
        }
        List<Attachment> attachmentList = attachmentManager.getAttachmentByBusinessId(id);
        if (attachmentList.size() > 0) {
            for (Attachment a:attachmentList) {
                attachmentManager.deleteAttachment(a);
            }
        }
        ztcSpecialRecommendationManager.delete(id);
        return JsonWrapper.successWrapper("删除成功！");
    }

    /**
     * 禁用推荐.
     * @return
     */
    @RequestMapping("stopSpecial")
    @ResponseBody
    public HashMap<String,Object> stopSpecial(String id){
        if(StringUtils.isBlank(id)){
            return JsonWrapper.failureWrapperMsg("参数错误，ID为空！");
        }
        ZtcSpecialRecommendation ztcSpecialRecommendation= ztcSpecialRecommendationManager.get(id);
        ztcSpecialRecommendation.setStatus(2);
        ztcSpecialRecommendationManager.save(ztcSpecialRecommendation);
        return JsonWrapper.successWrapper("操作成功！");
    }
}